﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using KBS8.Shared;

namespace KBS8.Data.GdfDataFilter
{
    class AreaFeatureReader: DatabaseReader
    {
        private string _query;
        public AreaFeatureReader(SqlCeConnection connection) : base(connection)
        {
            
        }

        /// <summary>
        /// Reads the AreaFeatures in the database
        /// </summary>
        /// <param name="o"></param>
        public override void Read(FilteredOutput o)
        {
            _query = @"SELECT AreaFeatureId, SplitIndicator, FeatureDefenitionId, MaxX, MaxY, MinX, MinY FROM AreaFeature " +
                "WHERE (" + o.Area.X + " < MaxX) AND " +
                "(MinX < " + (o.Area.X + o.Area.rec.Width) +") AND " +
                "(" + o.Area.Y + " < MaxY) AND " +
                "(MinY < " + o.Area.Y + o.Area.rec.Height + ")";

            Connection.Open();
            SqlCeCommand cmd = Connection.CreateCommand();
            cmd.CommandText = _query;
            SqlCeDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                AreaFeature tmp = new AreaFeature();
                tmp.AreaId = Convert.ToInt32(reader["AreaFeatureId"]);
                tmp.SplitIndicator = Convert.ToInt32(reader["SplitIndicatorId"]);
                tmp.FeatureId = Convert.ToInt32(reader["FeatureDefenitionId"]);
                tmp.MaxX = Convert.ToInt32(reader["MaxX"]);
                tmp.MinX = Convert.ToInt32(reader["MinX"]);
                tmp.MaxY = Convert.ToInt32(reader["MaxY"]);
                tmp.MinY = Convert.ToInt32(reader["MinY"]);
                o.AreaFeatures.Add(tmp);
            }
            Connection.Close();

        }
    }
}
